home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Disc to the Future 2
/
Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin
/
MAC
/
MPW_TOOL
/
TOOLS
/
TOOLS_WI
/
BYACC__
/
BYACCPAR
< prev
next >
Wrap
Text File
|
1989-11-19
|
6KB
|
348 lines
/************************************************************************
* *
* An LR(1) Parser *
* *
* by *
* *
* Robert Paul Corbett *
* *
* 1986 November 15 *
* *
************************************************************************/
@@
#ifndef YYSTACKSIZE
#define YYSTACKSIZE 512
#endif
extern int yylex();
extern yyerror();
extern int write();
int yydebug;
int yypatched;
int yynchars;
int yychar, yychar2;
YYSTYPE yyval, yylval, yylval2;
short yycs[YYSTACKSIZE];
short yyss[YYSTACKSIZE];
YYSTYPE yyvs[YYSTACKSIZE];
short *yyssp;
YYSTYPE *yyvsp;
yyverbose(s, n)
register char *s;
register int n;
{
char message[32];
char number[20];
register char *t;
t = message;
while (*t++ = *s++);
if (n <= 0)
*t++ = '0';
else
{
s = number + 19;
*s = 0;
while (n > 0)
{
*--s = '0' + (n % 10);
n = n / 10;
}
while (*t++ = *s++);
}
*t++ = '\n';
(void) write(2, message, t - message);
}
int
yyparse()
{
register int yyx;
register int yyc;
register int yyn;
register int yystate;
register short *yysp;
register YYSTYPE *yyvp;
register int yystatus;
register short *yytmp;
#ifdef YYDEFCHK
register int yyok;
yyok = 0;
#endif
yystatus = 0;
yynchars = 0;
yystate = 0;
yysp = yyss;
yyvp = yyvs;
*yysp = 0;
for (;;)
{
yyx = yyrowbase[yystate];
if (yyx >= 0)
goto yyreduce;
if (yynchars > 0)
yyc = yychar;
else
{
yyc = yylex();
if (yyc < 0)
yyc = 0;
yychar = yyc;
yynchars = 1;
}
yyx += yyc;
if (yyx <= 0)
{
yyx += YYTABLESIZE;
if (yyx >= 0 && yycheck[yyx] == yyc)
{
yyx = yytable[yyx];
if (yyx > 0)
goto yyshift;
yyx = -yyx;
goto yyreduce;
}
}
yyx = yydefred[yystate];
#ifdef YYDEFCHK
if (yyok)
goto yyreduce;
if (yyx >= 0)
{
yytmp = yysp;
yyn = -1;
for (;;)
{
yyn -= yyrhslm1[yyx];
if (yyn > 0)
{
if (yyn >= YYSTACKSIZE)
return (2);
yystate = yycs[yyn - 1];
}
else
{
yysp += yyn;
yystate = *yysp;
yyn = 0;
}
yyc = yylhs[yyx];
yyx = yycolumnbase[yyc] + yystate;
if (yyx >= 0 && yyx <= YYTABLESIZE && yycheck[yyx] == yystate)
yystate = yytable[yyx];
else
yystate = yydefgoto[yyc];
yycs[yyn] = yystate;
yyx = yyrowbase[yystate];
if (yyx >= 0)
continue;
yyc = yychar;
yyx += yyc;
if (yyx <= 0)
{
yyx += YYTABLESIZE;
if (yyx >= 0 && yycheck[yyx] == yyc)
{
yyx = yytable[yyx];
if (yyx > 0)
{
yyacceptable:
yyok = 1;
yysp = yytmp;
yystate = *yysp;
yyx = yydefred[yystate];
goto yyreduce;
}
yyx = -yyx;
continue;
}
}
if (yystate == YYQFINAL && yyc == 0)
goto yyacceptable;
yysp = yytmp;
yystate = *yysp;
break;
}
}
#else
if (yyx >= 0)
goto yyreduce;
#endif
if (yystate == YYQFINAL && yyc == 0)
return (0);
if (yystatus > 0 || yyc == YYERRCODE)
{
if (yyc == 0)
return (1);
yystatus = 3;
if (yydebug && yyc != YYERRCODE)
yyverbose("discard token ", yyc);
yynchars--;
if (yynchars > 0)
{
yychar = yychar2;
yylval = yylval2;
}
continue;
}
yyssp = yysp;
yyvsp = yyvp;
yypatched = 0;
yyerror("syntax error");
yysp = yyssp;
yyvp = yyvsp;
yystate = *yysp;
if (yypatched)
continue;
yyrecover:
if (yydebug)
(void) write(2, "syntax error detected\n", 22);
yytmp = yysp;
yyn = -1;
for (;;)
{
yyx = yyrowbase[yystate];
if (yyx >= 0)
goto yyereduce;
yyx += YYERRCODE;
if (yyx <= 0)
{
yyx += YYTABLESIZE;
if (yyx >= 0 && yycheck[yyx] == YYERRCODE)
{
yyx = yytable[yyx];
if (yyx > 0)
break;
yyx = -yyx;
goto yyereduce;
}
}
yyx = yydefred[yystate];
if (yyx >= 0)
goto yyereduce;
yysp = yytmp;
if (yydebug)
yyverbose("pop state ", *yysp);
yysp--;
if (yysp < yyss)
return (1);
yytmp = yysp;
yystate = *yysp;
continue;
yyereduce:
yyn -= yyrhslm1[yyx];
if (yyn > 0)
{
if (yyn > YYSTACKSIZE)
return (2);
yystate = yycs[yyn - 1];
}
else
{
yysp += yyn;
yystate = *yysp;
yyn = 0;
}
yyc = yylhs[yyx];
yyx = yycolumnbase[yyc] + yystate;
if (yyx >= 0 && yyx <= YYTABLESIZE && yycheck[yyx] == yystate)
yystate = yytable[yyx];
else
yystate = yydefgoto[yyc];
}
yysp = yytmp;
yyvp = yyvs + (yysp - yyss);
yystate = *yysp;
yynchars = 2;
yychar2 = yychar;
yylval2 = yylval;
yychar = YYERRCODE;
#ifdef YYDEFCHK
yyok = 1;
#endif
yystatus = 3;
continue;
yysignal:
yysp += yyn;
yyvp += yyn;
goto yyrecover;
yyshift:
if (yysp >= yyss + YYSTACKSIZE)
return (2);
*++yysp = yystate = yyx;
*++yyvp = yylval;
#ifdef YYDEFCHK
yyok = 0;
#endif
if (yystatus > 0)
yystatus--;
if (yydebug)
yyverbose("shift state ", yystate);
yynchars--;
if (yynchars > 0)
{
yychar = yychar2;
yylval = yylval2;
}
continue;
yyreduce:
if (yydebug)
yyverbose("reduce rule ", yyx);
yyn = yyrhslm1[yyx];
yysp -= yyn;
yyvp -= yyn;
if (yysp >= yyss + YYSTACKSIZE)
return (2);
switch (yyx)
{
#define YYACCEPT return (0)
#define YYERROR goto yysignal
#define yyerrok yystatus = 0
#define yyclearin yynchars = 0
@@
default:
goto yydonothing;
}
*yyvp = yyval;
yydonothing:
yystate = yysp[-1];
yyc = yylhs[yyx];
yyx = yycolumnbase[yyc] + yystate;
if (yyx >= 0 && yyx <= YYTABLESIZE && yycheck[yyx] == yystate)
yystate = yytable[yyx];
else
yystate = yydefgoto[yyc];
*yysp = yystate;
}
}